<template>
  <el-form ref="postForm" :model="postForm" :rules="rules">
    <sticky :class-name="'sub-navbar ' + postForm.status">
      <el-button @click="showGuide">帮助中心</el-button>
      <el-button v-loading="loading" type="success" @click="submitForm">{{ isEdit?'编辑电子书':'新增电子书' }}</el-button>
    </sticky>
    <div class="detail-container">
      <warning />
      <el-row>
        <el-col :span="24">
          <!-- 上传控件的具体样式 -->
          <ebook-upload
            :file-list="fileList"
            :disabled="isEdit"
            @onSuccess="onUploadSuccess"
            @onRemove="onUploadRemove"
          />
        </el-col>
        <el-col :span="24">
          <el-row>
            <el-form-item prop="title">
              <MDinput
                v-model="postForm.title"
                prop="title"
                style="margin-bottom:40px"
                type="text"
                placeholder="输入标题"
              >标题</MDinput>
            </el-form-item>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="作者:" :label-width="labelWidth" prop="author">
                <el-input v-model="postForm.author" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出版社:" :label-width="labelWidth" prop="publisher">
                <el-input v-model="postForm.publisher" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="语言:" :label-width="labelWidth" prop="language">
                <el-input v-model="postForm.language" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="根文件:" prop="rootFile" :label-width="labelWidth">
                <el-input v-model="postForm.rootFile" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="文件路径:" prop="filePath" :label-width="labelWidth">
                <el-input v-model="postForm.filePath" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="解压路径:" prop="unzipPath" :label-width="labelWidth">
                <el-input v-model="postForm.unzipPath" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="封面路径:" prop="coverPath" :label-width="labelWidth">
                <el-input v-model="postForm.coverPath" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="文件名称" prop="originalName" :label-width="labelWidth">
                <el-input v-model="postForm.originalName" disabled />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="封面" prop="cover">
                <a v-if="postForm.cover" :href="postForm.cover">
                  <img :src="postForm.cover" alt="封面">
                </a>
                <span v-else>无</span>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="目录">
                <div
                  v-if="contentsTree && contentsTree.length>0"
                  class="contents-wrapper"
                >
                  <el-tree :data="contentsTree" @node-click="onContentText" />
                </div>
                <span v-else>无</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </el-form>
</template>

<script>
import Sticky from '@/components/Sticky'
import Warning from './Warning'
import EbookUpload from '@/components/EbookUpload'
import MDinput from '@/components/MDinput'
import { createBook, getBook, updateBook } from '@/api/book'

const fields = {
  title: '书名',
  author: '作者',
  publisher: '出版社',
  language: '语言'
}
export default {
  name: '',
  components: {
    Sticky,
    Warning,
    EbookUpload,
    MDinput
  },
  props: {
    isEdit: {
      type: Boolean
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === undefined || value.length === 0) {
        callback(new Error(fields[rule.field] + '必须填写'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      postForm: {
        status: 'draft'
      },
      fileList: [],
      labelWidth: '120px',
      rules: {
        title: [{ validator: validateRequire }],
        author: [{ validator: validateRequire }],
        publisher: [{ validator: validateRequire }],
        language: [{ validator: validateRequire }]
      },
      contentsTree: []
    }
  },
  created() {
    if (this.isEdit) {
      const fileName = this.$route.params.fileName
      this.getBookData(fileName)
    }
  },
  methods: {
    getBookData(fileName) {
      getBook(fileName).then(response => {
        const { book } = response.data
        this.setData(book)
      })
    },
    setData(data) {
      const {
        title,
        author,
        publisher,
        language,
        rootFile,
        cover,
        originalName,
        url,
        contents,
        contentsTree,
        fileName,
        coverPath,
        filePath,
        unzipPath
      } = data
      this.postForm = {
        title,
        author,
        publisher,
        language,
        rootFile,
        cover,
        url,
        originalName,
        contents,
        fileName,
        coverPath,
        filePath,
        unzipPath
      }
      this.fileList = [{ name: originalName, url }]
      this.contentsTree = contentsTree
    },
    setDefault() {
      // this.postForm = Object.assign({}, defaultForm)
      this.fileList = []
      this.contentsTree = []
      this.$refs.postForm.resetFields()
    },
    onUploadSuccess(data) {
      this.setData(data)
    },
    onUploadRemove() {
      this.setDefault()
    },
    onContentText(data) {
      if (data.text) {
        window.open(data.text)
      }
    },
    showGuide() {
      console.log('help')
    },
    submitForm() {
      const onSuccess = (response) => {
        const { msg } = response
        console.log(msg)
        this.$notify({
          title: '操作成功',
          message: msg
        })
      }
      if (!this.loading) {
        this.loading = true
        this.$refs.postForm.validate((valid, fields) => {
          if (valid) {
            const book = Object.assign({}, this.postForm)

            if (!this.isEdit) {
              createBook(book).then(res => {
                onSuccess(res)
                this.loading = false
                this.setDefault()
              }).catch(() => {
                this.loading = false
              })
            } else {
              updateBook(book).then(response => {
                onSuccess(response)
                this.loading = false
              })
            }
          } else {
            const message = fields[Object.keys(fields)[0]][0].message
            this.$message({
              message,
              type: 'error'
            })
            this.loading = false
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-container {
  padding: 40px 45px 20px 50px;
}
</style>
